#ifndef _TIME_INCLUDED_
#define _TIME_INCLUDED_
/*
 *  time.h
 *  prace s casem a datumem
 */

struct Time {
	int sec, min, hour;
};

/*
 *  read_cmos()
 *  cte byte z CMOS
 */  
unsigned char read_cmos(int reg) {
  __outportb__(0x70, reg);
  return (unsigned char) __inportb__(0x71);
}

/*
 *  is_bcd()
 *  je to BCD?
 */  
char is_bcd() {
  return (read_cmos(0x0B) & 0x02);
}

/*
 *  read_bcd()
 *  cti byte z CMOS a zkonvertuj do BCD 
 */ 
char read_bcd(int reg) {
  int in = read_cmos(reg);
  if (is_bcd())
    return (in >> 4) * 10 + (in & 15);
  else
    return in;
}

/*
 *  gettime()
 *  zjisti aktualni cas
 */  
void gettime(struct Time * t) {
  if(t) {
    t->sec = read_bcd(0);
    t->min = read_bcd(2);
    t->hour= read_bcd(4);
  }
}

#endif
